package com.zifan.st_geometry.mapper;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.ibatis.jdbc.SQL;

import com.zifan.st_geometry.model.STGeometry;

public class STGeomertySqlBuilder {

	/**
	 * 
	 * @param srcTableName
	 *            源表表名字
	 * @param targetTableName
	 *            目标表的表名
	 * @param fields
	 *            返回表字段数据，如果两个表明字段都一样，要求添加前缀T或者S，例如T.OBJECTID
	 * @param where
	 *            原表数据条件，例如 objectId=100
	 * @return
	 */
	public String buildQueryIntersectByWhere(final String srcTableName, final String targetTableName,
			final String[] fields, final String where) {
		return new SQL() {
			{
				SELECT("SDE.ST_AREA(SDE.ST_TRANSFORM(S.SHAPE, #{srId})) SAREA");
				SELECT("SDE.ST_ASTEXT(SDE.ST_INTERSECTION(SDE.ST_TRANSFORM(T.SHAPE, #{srId}),SDE.ST_TRANSFORM(S.SHAPE, #{srId}))) OVERLAPCOORDS");
				SELECT("SDE.ST_AREA(SDE.ST_INTERSECTION(SDE.ST_TRANSFORM(T.SHAPE, #{srId}),SDE.ST_TRANSFORM(S.SHAPE, #{srId}))) OVERLAPAREA");
				if (fields != null) {
					for (String field : fields) {
						SELECT(field);
					}
				}
				FROM(srcTableName + " S").FROM(targetTableName + " T");
				WHERE("SDE.ST_INTERSECTS(T.SHAPE, S.SHAPE) = 1").AND().WHERE(where);
			}
		}.toString();
	}

	/**
	 * 
	 * @param targetTableName
	 *            叠加图层的表名
	 * @param fields
	 *            返回表字段数据
	 * @return
	 */
	public String buildQueryIntersectByWKT(final String targetTableName, final String[] fields) {
		return new SQL() {
			{
				SELECT("SDE.ST_AREA(SDE.ST_GEOMETRY(#{wkt,typeHandler=com.zifan.st_geometry.handler.STGeometryHandler}, #{srId})) SAREA");
				SELECT("SDE.ST_ASTEXT(SDE.ST_INTERSECTION(SDE.ST_TRANSFORM(SHAPE, #{srId}),SDE.ST_GEOMETRY(#{wkt,typeHandler=com.zifan.st_geometry.handler.STGeometryHandler}, #{srId}))) OVERLAPCOORDS");
				SELECT("SDE.ST_AREA(SDE.ST_INTERSECTION(SDE.ST_TRANSFORM(SHAPE, #{srId}),SDE.ST_GEOMETRY(#{wkt,typeHandler=com.zifan.st_geometry.handler.STGeometryHandler}, #{srId}))) OVERLAPAREA");
				if (fields != null) {
					for (String field : fields) {
						SELECT(field);
					}
				}
				FROM(targetTableName);
				WHERE("SDE.ST_INTERSECTS(SHAPE, SDE.ST_GEOMETRY(#{wkt,typeHandler=com.zifan.st_geometry.handler.STGeometryHandler}, #{srId})) = 1");
			}
		}.toString();
	}

	public String buildInsertSql(final STGeometry geometry)
			throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
		return new SQL() {
			{
				INSERT_INTO(geometry.getTableName());
				Field[] fieldList = geometry.getClass().getDeclaredFields();
				for (Field field : fieldList) {
					String methodName = "get".concat(field.getName().substring(0, 1).toUpperCase())
							.concat(field.getName().substring(1));
					Method method = geometry.getClass().getMethod(methodName);
					if (method.invoke(geometry) != null) {
						VALUES(field.getName(), "#{" + field.getName() + "}");
					}
				}
				VALUES("SHAPE",
						"SDE.ST_GEOMETRY(#{wkt,typeHandler=com.zifan.st_geometry.handler.STGeometryHandler}, #{srId})");
				VALUES("OBJECTID", "#{objectId}");
			}

		}.toString();
	}

	public String builderGetCountSql(final String tableName) {
		return new SQL() {
			{
				SELECT("count(*) as c");
				FROM(tableName);
			}
		}.toString();
	}
}
